package main

import (
	"fmt"
	"strings"
	"strconv"
	"flag"
	"math/rand"
	"time"
	"net/url"
	"os"
)

var ualist = []string{
	"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60",
	"Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
	"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
	"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36",
	"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16",
	"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36",
	"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11",
	"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
	"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)",
	"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.3.4000 Chrome/30.0.1599.101 Safari/537.36",
	"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
}


type resoure struct {
	url      string
	target   string
	start    int
	end      int
}

//数据
func ruleResoure() []resoure  {

	var res []resoure
	r1 :=  resoure{
		url:"http://www.ktvsong.com/",
		target:"",
		start:0,
		end:0,
	}

	r2 :=  resoure{
		url:"http://www.ktvsong.com/download?cate={$id}",
		target:"{$id}",
		start:0,
		end:7,
	}

	r3 :=  resoure{
		url:"http://www.ktvsong.com/content?songid={$id}",
		target:"{$id}",
		start:235525,
		end:325525,
	}

	res = append(append(append(res, r1), r2), r3)

    return  res
}

func buildurl(res []resoure)  []string{

	    var weburl []string
	    for _,resItem :=range res {
	    	  if resItem.target == ""{
				  weburl=  append(weburl, resItem.url)
			  }else{
				  for i:=resItem.start; i<= resItem.end ; i++ {
					   url := strings.Replace(resItem.url,resItem.target,strconv.Itoa(i),-1)
					   weburl = append(weburl, url)
				  }
			  }
		}
		return  weburl
}

func randInt(min,max int) int {
	r := rand.New( rand.NewSource(time.Now().UnixNano())) //加一个时间的种子，不然会出现固定值
	if min > max {
		return  max
	}
    return  r.Intn(max-min) + min
}

//组装日志内容
func makeLog(current, refer, ua string) string {
	//Values将建映射到值的列表。它一般用于查询的参数和表单的属性。不同于http.Header这个字典类型，Values的键是大小写敏感的。
	u := url.Values{}
	u.Set("time","1")
	u.Set("url",current)
	u.Set("refer",refer)
	u.Set("ua",ua)
	paramStr := u.Encode()
	logTemplate := "211.87.152.44 – - [18/Mar/2005:12:21:42 +0800] \"GET  {$paramStr} /HTTP/1.1\" 200 899 \"http://www.baidu.com\" \"{$ua}\" "
	log := strings.Replace(logTemplate,"{$paramStr}",paramStr,-1)
	log =  strings.Replace(log,"{$ua}",ua,-1)
    return  log
}


func main()  {

	total :=flag.Int("total",100,"how many row by created:")
	filePath := flag.String("filePath","D:/goweb/src/test/run/web.log","input filePaht: ")

	//构造真实的网站集合
    res := ruleResoure()
    list := buildurl(res)

    logStr := ""
    //按要求 生成日志内容
    for i:=0;i<*total; i++{

    	currentUrl := list[randInt(0,len(list)-1)]
    	referUrl := list[randInt(0,len(list)-1)]
    	ua := ualist[randInt(0,len(ualist)-1)]

    	logStr  = logStr + makeLog( currentUrl,referUrl,ua) + "\n"

	}

	fd,_ := os.OpenFile(*filePath, os.O_RDWR| os. O_CREATE |os.O_APPEND, 0644)
	fd.Write([]byte(logStr))
	fd.Close()

	fmt.Println("log created finish")

}
